package org.shan.hangtian.ui;

import org.shan.hangtian.properties.HangtianProperties;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class SettingDialog extends JDialog {

    private JButton select;

    private JTextField textField;

    public SettingDialog() {
        setBounds((MainFrame.SCREEN_W - 400) / 2, (MainFrame.SCREEN_H - 300) / 2, 400, 200);
        setLayout(new BorderLayout());

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        panel.setPreferredSize(new Dimension(0, 40));
        panel.setBorder(new EmptyBorder(20, 20, 0, 0));
        {
            JLabel label = new JLabel("文件路径：");
            label.setFont(new Font("黑体", Font.PLAIN, 18));
            label.setForeground(new Color(40, 40, 40));
            panel.add(label);
        }
        add(panel, BorderLayout.NORTH);

        panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        panel.setBorder(new EmptyBorder(20, 20, 0, 0));
        {
            textField = new JTextField(HangtianProperties.getInstance().getPath());
            textField.setBorder(new LineBorder(new Color(40, 40, 40)));
            textField.setColumns(30);
            textField.setEditable(false);
            panel.add(textField);
        }
        add(panel, BorderLayout.CENTER);

        panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
        panel.setPreferredSize(new Dimension(0, 60));
        panel.setBorder(new EmptyBorder(0, 20, 0, 0));
        {
            select = new JButton("选择目录");
            select.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            select.setBackground(new Color(69, 142, 253));
            select.setForeground(new Color(255, 255, 255));
            select.setFont(new Font("黑体", Font.PLAIN, 16));
            select.setFocusPainted(false);
            panel.add(select);
        }
        add(panel, BorderLayout.SOUTH);

        addListeners();
    }

    private void addListeners() {
        select.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.setDialogTitle("请选择数据保存目录：");
                chooser.setFileSelectionMode(1);
                int n = chooser.showDialog(SettingDialog.this, "选择");
                if (n == 0) {
                    File file = chooser.getSelectedFile();
                    try {
                        HangtianProperties.getInstance().setPath(file.getAbsolutePath());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            textField.setText(file.getAbsolutePath());
                            textField.updateUI();
                        }
                    });
                }
            }
        });
    }
}
